package net.zjitc.controller;

import com.mysql.jdbc.NotUpdatable;
import net.zjitc.pojo.*;
import net.zjitc.service.AdminProductService;
import net.zjitc.service.CartService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.OrderService;
import net.zjitc.service.impl.AdminProductServiceImpl;
import net.zjitc.service.impl.CartServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.OrderServiceImpl;
import net.zjitc.utils.CommonUtils;
import net.zjitc.utils.dispacher.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/12/13.
 */
public class UserOrder {
    CartService cartService=new CartServiceImpl();
    OrderService orderService=new OrderServiceImpl();
    AdminProductService productService=new AdminProductServiceImpl();
    //提交订单
    @URLMapping(url = "/createOrder")
    public String createOrder(HttpServletRequest request, HttpServletResponse response){
        /**
         * 1.获取session user
         * 2.生成一个订单对象 order(id,uuid,时间，未付款，收货人信息，用户)
         * 3.订单根据购物车生成的（从数据库中查出购物车的信息cartlist）
         * 4.根据遍历cartlist购物车条目信息，生成订单条目（订单总价格total）
         * 5.订单条目是个集合，要把它放在订单下面
         * 6.把order订单插入数据库（事务）
         * 7.购物车清空
         * 8.order放到request域中，转发到订单页面
         * */
        //1.获取session user
        User user= (User) request.getSession().getAttribute("session_user");
        if (user==null){
            return "Redirect:"+request.getContextPath()+"/loginUI";
        }
        //2.生成一个订单对象
        Order order=new Order();
        order.setOid(CommonUtils.uuid());
        //设置收货人信息
        order.setAddr(user.getAddr());
        order.setPhone(user.getPhone());
        order.setName(user.getName());

        order.setState(1);//1 未付款 2 已付款但未发货 3 已发货但未收货 4 收货交易完成
        order.setUser(user);
        //生成日期（日期的）
        order.setOrdertime(CommonUtils.dateToString(new Date()));
        //3.订单根据购物车生成的（从数据库中查出购物车的信息cartlist）
        List<CartItem> cartItemList=cartService.findByUid(user.getUid());
        //如果购物车为空则不执行下列操作
        if (cartItemList== null){
            return "/WEB-INF/user/order.jsp";
        }
        //4.根据遍历cartlist购物车条目信息，生成订单条目（订单总价格total）
        List<Orderitem> orderitemList=new ArrayList<Orderitem>();//用于存入订单条目
        double total=0.0;//初始化价格
        for (CartItem item:cartItemList){
            Orderitem orderitem=new Orderitem();
            orderitem.setOrder(order);
            orderitem.setProduct(item.getProduct());
            orderitem.setOsatate(0);//评价状态
            orderitem.setCount(item.getQuantity());//设置数量
            orderitem.setSubtotal(item.getProduct().getShop_price()*item.getQuantity());
            total=total+item.getProduct().getShop_price()*item.getQuantity();//计算总价
            orderitemList.add(orderitem);
        }
        //5.订单条目是个集合，要把它放在订单下面
        order.setOrderitemList(orderitemList);
        order.setTotal(total);
        //6.把order订单插入数据库（事务）+购物车清空(同个事务)
        int count=orderService.add(order);
        //7.购物车清空
        //cartService.clearCart(user);
        //8.order放到request域中，转发到订单页面
        request.setAttribute("order",order);
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/order.jsp";
    }

    //我的订单
    @URLMapping(url = "/myOrder")
    public String shopIndex(HttpServletRequest request, HttpServletResponse response){
        //1.获取session user
        User user= (User) request.getSession().getAttribute("session_user");
        if (user==null){
            return "Redirect:"+request.getContextPath()+"/loginUI";
        }
        //通过用户找到订单
        List<Order> orders=orderService.findByUid(user.getUid());
        request.setAttribute("orders",orders);
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/orderList.jsp";
    }
    //付款
    @URLMapping(url = "/order_findByOid")
    public String order_findByOid(HttpServletRequest request, HttpServletResponse response){
        String oid=request.getParameter("oid");
        int count=orderService.editPayState(oid);
        return "Redirect:"+request.getContextPath()+"/myOrder";
    }
    //确认收货
    @URLMapping(url = "/order_editOrder")
    public String order_editOrder(HttpServletRequest request, HttpServletResponse response){
        String oid=request.getParameter("oid");
        int count=orderService.editStateFour(oid);
        //将订单项的ostate改为去评价
        int countostate=orderService.editOstate(oid);
        return "Redirect:"+request.getContextPath()+"/myOrder";
    }
    //跳转到评价页面
    @URLMapping(url = "/evaluate_findByPid")
    public String evaluate_findByPid(HttpServletRequest request, HttpServletResponse response){
        //获取产品
        String pid=request.getParameter("pid");
        Product product=productService.findByPid(pid);
        request.setAttribute("product",product);
        //获取订单项id
        String itemid=request.getParameter("itemid");
        request.setAttribute("itemid",itemid);
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/addEvaluate.jsp";
    }
    //添加评价
    @URLMapping(url = "/evaluate_saveEval")
    public String evaluate_saveEval(HttpServletRequest request, HttpServletResponse response){
        //获取产品
        String pid=request.getParameter("pid");
        Product product=productService.findByPid(pid);
        //request.setAttribute("pid",pid);
        //获取title(好评，中评，差评)
        String title=request.getParameter("title");
        //获取评价内容
        String content=request.getParameter("content");
        //获取session user
        User user= (User) request.getSession().getAttribute("session_user");
        //设置评价
        Evaluate evaluate=new Evaluate();
        evaluate.setProduct(product);
        evaluate.setUser(user);
        evaluate.setContent(content);
        evaluate.setTitle(title);
        evaluate.setDate(new Date());
        //插入评价
        int count=productService.addEvaluate(evaluate);
        //获取订单项id
        String itemid=request.getParameter("itemid");
        int countItemid=orderService.editOstateByItemid(itemid);
        return "Redirect:"+request.getContextPath()+"/productFindByPid?pid="+pid;
        //return "/shoptest/productFindByPid";
    }
}

















